package com.hans.scala

/**
 * 提取器
 * Created by Hans on 2014/4/13.
 */
class Name(val f: String, val l: String)

object Name2 {
  def unapplySeq(input: String): Option[Seq[String]] =
    if (input == "") None else Some(input.trim.split("\\s+"))
}

object Name {
  def unapplySeq(input: String): Option[Seq[String]] =
    if (input == "") None else Some(input.trim.split("\\s+"))

  def main(args: Array[String]) {
    val Name(first, last) = Name("li xiaoyao")
    println(first + "," + last)

    val Name(first1, last1) = "li xiaoyao"
    println(first1 + "," + last1)

    "li xiaoyao" match {
      case Name(a1, a2) => println(a1 + "," + a2)
      case Name2(a1, a2, a3) => println(a1 + "," + a2 + "," + a3)
    }

    matcher("li xiao yao")
    matcher("li xiao yao hans")

    val Name2(t1, t2, t3) = "li xiao yao"
    println(t1 + "," + t2 + "," + t3)
  }

  def apply(input: String): Name = {
    val pos = input.split(" ")
    new Name(pos(0), pos(1))
  }

  def unapply(n: Name): Option[(String, String)] = Some((n.f, n.l))

  def unapply(input: String): Option[(String, String)] = {
    val pos = input.split(" ")
    Some((pos(0), pos(1)))
  }

  def matcher(name: String) {
    name match {
      case Name2(a1, a2) => println(a1 + "," + a2)
      case Name2(a1, a2, a3) => println(a1 + "," + a2 + "," + a3)
      case Name2(a1, a2, a3, a4) => println(a1 + "," + a2 + "," + a3 + "," + a4)
    }
  }
}
